﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.model;
using mars_server.csharp.cache;
using mars_server.csharp.socket;

namespace mars_server.csharp.dao.impl
{
    public class UserSocketManagerImpl : UserSocketManager
    {
        //单例模式
        private UserSocketManagerImpl() { }
        private static readonly UserSocketManagerImpl instance = new UserSocketManagerImpl();
        public static UserSocketManagerImpl Instance
        {
            get
            {
                return instance;
            }
        }



        public bool deleteUserSocketByUserId(long userId)
        {
            for (int i = UserSocketCache.UserSocketList.Count - 1; i >= 0; i--)
            {
                if (UserSocketCache.UserSocketList[i] != null && UserSocketCache.UserSocketList[i].userDO != null)
                {
                    if (userId == UserSocketCache.UserSocketList[i].userDO.id)
                    {
                        UserSocketCache.UserSocketList.Remove(UserSocketCache.UserSocketList[i]);
                        return true;
                    }
                }
            }
            return false;
        }

        public bool addUserSocket(UserSocket userSocket)
        {
            throw new NotImplementedException();
        }


        /**
         * 从UserSocket中判断是否存在该用户的Socket
         * */
        public UserSocket selectUserSocketByUserId(long userId)
        {
            for (int i = 0; i < UserSocketCache.UserSocketList.Count; i++)
            {
                if (UserSocketCache.UserSocketList[i] != null && UserSocketCache.UserSocketList[i].userDO != null)
                {
                    if (userId == UserSocketCache.UserSocketList[i].userDO.id)
                    {
                        return UserSocketCache.UserSocketList[i];
                    }
                }
            }
            return null;
        }


        /**
         * 从userlistCache中删除指定的UserSocket
         * */
        public bool deleteUserSocketByUserSocketDO(UserSocket userSocketDO)
        {
            for (int i = UserSocketCache.UserSocketList.Count - 1; i >= 0; i--)
            {
                if (UserSocketCache.UserSocketList[i] != null)
                {
                    if (userSocketDO == UserSocketCache.UserSocketList[i])
                    {
                        UserSocketCache.UserSocketList.Remove(UserSocketCache.UserSocketList[i]);
                        return true;
                    }
                }
            }
            return false;
        }


    }
}
